<template>
  <el-drawer v-model="drawer" title="查找订单" size="50%">
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryOrderFormRef"
      :inline="true"
      label-width="80px"
    >
      <el-form-item label="" prop="queryValue">
        <el-input v-model="queryParams.queryValue" placeholder="请输入订单号/昵称/手机号" />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="orderList" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="订单号" align="center" prop="code" />
      <el-table-column label="客户昵称" align="center" prop="memberName" />
      <el-table-column label="手机号" align="center" prop="memberPhone" />
      <el-table-column label="订单金额" align="center" prop="actualPayment">
        <template #default="scope">
          {{ (scope.row.actualPayment / 100).toFixed(2) }}
        </template>
      </el-table-column>
      <el-table-column label="是否已退款" align="center" prop="refundStatus">
        <template #default="scope">
          {{ scope.row.refundStatus ? '是' : '否' }}
        </template>
      </el-table-column>
      <el-table-column label="项目" align="center" prop="projectName">
        <template #default="scope">
          {{ scope.row.remark ? JSON.parse(scope.row.remark).projectName : '' }}
        </template>
      </el-table-column>
      <el-table-column
        label="支付时间"
        align="center"
        prop="payFinishTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            type="primary"
            v-if="!scope.row.refundStatus"
            @click="openForm(scope.row.code)"
            v-hasPermi="['meijia:order-refund:create']"
          >
            确认退款
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      :total="orderTotal"
      v-model:page="orderQueryParams.pageNo"
      v-model:limit="orderQueryParams.pageSize"
      @pagination="orderList"
    />
  </el-drawer>
  <OrderRefundForm ref="formRef" @success="getOrderList" />
</template>
<script setup lang="ts">
import { DICT_TYPE, getIntDictOptions, getStrDictOptions } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import OrderRefundForm from './OrderRefundForm.vue'
import * as OrderApi from '@/api/meijia/order'
import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗
const { wsCache } = useCache()
const user = wsCache.get(CACHE_KEY.USER)
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  queryValue: undefined
})
const dialogVisible = ref(false) // 弹窗的是否展示
const activeName = ref('first')
const drawer = ref(false)
const queryOrderFormRef = ref() // 搜索的表单
const orderList = ref([]) // 订单列表
const orderTotal = ref(0) // 订单列表的总页数
const orderQueryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  queryValue: undefined
})
/** 打开弹窗 */
const open = async (id: number) => {
  drawer.value = true
}

const getOrderList = async () => {
  const data = await OrderApi.pageQuery(queryParams)
  orderList.value = data.list
  orderTotal.value = data.total
}
const handleRefundDialog = async () => {
  dialogVisible.value = true
}
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getOrderList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryOrderFormRef.value.resetFields()
  orderList.value = []
  orderTotal.value = 0
}
const formRef = ref()
const openForm = (code: string) => {
  formRef.value.open(code)
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗
</script>
<style scoped>
.el-select {
  width: 100%;
}
.info_item,
.mr-5px {
  margin-right: 10px;
}
.img {
  margin-left: 20px;
}
</style>
